﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using IHISCETeim.Model;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using FineUI;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Data;
namespace IHISCETeim.Web.AgentSys.SvrMgr
{
    public partial class AgentQltyVisitList : AgentPageBase
    {

        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get { return "AgentQltyVisitList"; }
        }

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
           // CheckPowerWithButton(Permission.新增, btnNew);
           // CheckPowerWithButton(Permission.导出, btnExecl);

            
            // 每页记录数
            Grid1.PageSize = PageSize;
            ddlGridPageSize.SelectedValue = PageSize.ToString();

            BindGrid();
        }


        protected string GetSQLWhere()
        {
            string SeaVal = ttbSearchMessage.Text.Trim();

            string wheresql = "";

            string sqlcommonFiled = "";
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("ClaimsNumber", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " or " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("SerialNumber", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " or " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }
            if (!string.IsNullOrEmpty(SeaVal))
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("CustomerName", QueryOperatorsEnum.包含, SeaVal);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " or " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            if (this.rblIsVisit.SelectedValue != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("IsVisit", QueryOperatorsEnum.等于,this.rblIsVisit.SelectedValue);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }

            if (this.FromRealDate.Text != "")
            {
                sqlcommonFiled = QueryOperatorsEnumHelper.ConvertCommonOperator("RealDate", QueryOperatorsEnum.范围, this.FromRealDate.Text + "|" + this.ToRealDate.Text);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + sqlcommonFiled;
                }
                else
                {
                    wheresql = sqlcommonFiled;
                }
            }


            return wheresql;
        }

        private void BindGrid()
        {
          
            Grid1.DataSource = AgentQltyVisitController.GetPagination(GetSQLWhere(), Grid1.PageIndex + 1, Grid1.PageSize, Grid1.SortField + " " + Grid1.SortDirection, ref TotalCount);
            Grid1.RecordCount = TotalCount;
            Grid1.DataBind();

        }

        #endregion

        #region Events

        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            ttbSearchMessage.ShowTrigger1 = true;
            BindGrid();
        }

        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            ttbSearchMessage.Text = String.Empty;
            ttbSearchMessage.ShowTrigger1 = false;
            BindGrid();
        }
        protected void Grid1_PreDataBound(object sender, EventArgs e)
        {
            // 数据绑定之前，进行权限检查
            CheckPowerWithLinkButtonField(Permission.删除, Grid1, "deleteField");
           // CheckPowerWithLinkButtonField(Permission.修改, Grid1, "visitField");
            CheckPowerWithWindowField(Permission.查看, Grid1, "visitField");

        }

        protected void Grid1_PreRowDataBound(object sender, FineUI.GridPreRowEventArgs e)
        {
            //DataRowView objVisit = e.DataItem as DataRowView;

            //// 不能
            //if (Convert.ToBoolean(objVisit["IsVisit"]))
            //{
            //    FineUI.WindowField visitField = Grid1.FindColumn("visitField") as FineUI.WindowField;
            //    visitField.Enabled = false;
            //    visitField.ToolTip = "已回访,无需再回访！";
            //}

        }
        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortDirection = e.SortDirection;
            Grid1.SortField = e.SortField;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }


        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            int ID = GetSelectedDataKeyID(Grid1);
            if (e.CommandName == "Delete")
            {
                // 在操作之前进行权限检查
                if (!HasPower(Permission.删除))
                {
                    CheckPowerFailWithAlert();
                    return;
                }
                //执行删除代码  
                if (AgentQltyVisitController.Delete(ID))
                {
                    BindGrid();
                }
                else
                {
                    Alert.Show("删除失败");
                    return;
                }               
            }
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void ddlGridPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            Grid1.PageSize = Convert.ToInt32(ddlGridPageSize.SelectedValue);
            // 更改每页显示数目时，防止 PageIndex 越界
            if (Grid1.PageIndex > Grid1.PageCount - 1)
            {
                Grid1.PageIndex = Grid1.PageCount - 1;
            }
            BindGrid();
        }

        #endregion

        protected void Grid1_RowDataBound(object sender, GridRowEventArgs e)
        {

            //DataRowView dr = e.DataItem as DataRowView;
            //try
            //{
            //    string ClaimStateVal = dr["ClaimState"].ToString();
            //    System.Web.UI.WebControls.HyperLink xx = Grid1.Rows[e.RowIndex].FindControl("hlClaimState") as System.Web.UI.WebControls.HyperLink;
            //    System.Web.UI.WebControls.HyperLink hlopenClaims = Grid1.Rows[e.RowIndex].FindControl("openClaims") as System.Web.UI.WebControls.HyperLink;
            //    if (ClaimStateVal == ClaimState.品情驳回.ToString())
            //    {
            //        xx.ToolTip = dr["CancelReason"].ToString();
            //    }
            //    if (HasPower(new Permission[] { Permission.修改, Permission.查看 }))
            //    {
            //        hlopenClaims.NavigateUrl = "javascript:openAgentQltySvrAct(" + dr["QltyId"] + ",'" + dr["ClaimsNumber"] + "')";
            //    }
            //    //
            //    // CheckPowerFailWithLinkButtonField(Grid1, "btnView");

            //}
            //catch
            //{

            //}
        }

        protected void rblSearchSvrAct_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (FromRealDate.Text == "" || ToRealDate.Text == "")
            {
                FromRealDate.Text = "";
                ToRealDate.Text = "";                
            }
            BindGrid();
        }

        protected void btnExecl_Click(object sender, EventArgs e)
        {
            // string url = this.ExportExcelWebUrl+"/AgentSys/AgentQlty.aspx?SearchMessage={0}&SearchSvrAct={1}&FromFaultDate={2}&ToFaultDate={3}";
           // string url = this.ExportExcelWebUrl + "/AgentSys/AgentQlty.aspx?q=" + HttpUtility.UrlEncode(GetSQLWhere());
            // url = string.Format(url, ttbSearchMessage.Text, rblSearchSvrAct.SelectedValue, FromFaultDate.Text, ToFaultDate.Text);

           // PageContext.RegisterStartupScript("top.window.open('" + url + "', '34')");

            //PageContext.Redirect(url, "_top");
        }
    }
}